Blog

Sending transactional emails using Mailcoach API in an Express.js application

Mailcoach is well known and used within the PHP and Laravel community. But Mailcoach capabilities also extend to JavaScript-driven apps through its transactional emails API endpoint.

Transactional emails play an important role in user engagement, often triggered by a specific user action such as account creation, purchase confirmation, or password resets.

In this article, we will look into sending transactional emails in an Express.js application.

Using Mailcoach in PHP or Laravel environments is easy thanks to our packages spatie/laravel-mailcoach-mailer for Laravel applications and spatie/mailcoach-mailer for PHP applications.

However, integrating Mailcoach into a JavaScript application needs a different approach. Instead of using the above packages, you will interact with Mailcoach via its API endpoint /api/transactional-mails/send. Here’s a simplified example of how you might send a request using curl:

$ MAILCOACH_API_TOKEN="your API token"
$ curl -X POST https://your-mailcoach-domain.com/api/transactional-mails/send \
    -H "Authorization: Bearer $MAILCOACH_TOKEN" \
    -H 'Accept: application/json' \
    -H 'Content-Type: application/json' \
    -d '{"mail_name":"name-of-the-mail", "subject": "Subject of your email", ...}'

In this example, we are including two parameters: mail_name and subject. You can customize the request further by adding the from parameter to set the sender’s address and the to parameter to specify one or more recipients, separated by commas.

For the full list of parameters, refer the the documentation.

What might an Express implementation look like?

Let’s have a look at an example:

const express = require('express');
const axios = require('axios');

const app = express();

const PORT = process.env.PORT || 3000;
const MAILCOACH_API_URL = 'https://your-mailcoach-domain.com/api/transactional-mails/send';

// Store this in your environment configuration rather than directly in code:
const MAILCOACH_API_TOKEN = 'your-mailcoach-api-token';

app.post('/send-email', async (req, res) => {
    try {
        const { mail_name, subject, to, from } = req.body;

        const response = await axios.post(MAILCOACH_API_URL, {
            mail_name,
            subject,
            to,
            from
        }, {
            headers: {
                'Authorization': `Bearer ${MAILCOACH_API_TOKEN}`,
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            }
        });

        const responseData = response.data;
        res.status(200).json({ message: 'Email sent successfully', data: responseData });
    } catch (error) {
        console.error('Error sending email:', error.message);
        res.status(500).json({ error: 'Internal server error' });
    }
});

In this example, we have created an Express route /send-email to handle POST requests for sending transactional emails. When a POST request is made to this endpoint, the server extracts necessary parameters from the request body (mail_name, subject, to, from, etc.), constructs a request payload, and sends a POST request to the Mailcoach API endpoint using Axios.

By implementing this route in your Express application, you can easily use Mailcoach for sending transactional emails. Check out our documentation to learn more about Mailcoach and all its uses, and start your free trial now.

Ready to get started?